layui.use(['form', 'layedit', 'laydate','upload','layer'], function(){
    var form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate
        ,upload = layui.upload;
    //自定义验证规则
    form.verify({
        title: function(value){
            if(value.length < 3){
              return "用户名至少3个字符";
            }
            if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                return '用户名不能有特殊字符';
            }
            if(/^\d+\d+\d$/.test(value)){
                return '用户名不能全为数字';
            }
        }
        ,pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
        ,content: function(value){
            layedit.sync(editIndex);
        }
    });
    //监听提交
    form.on('submit(demo1)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '最终的提交信息'
        })
        return false;
    });
    $=layui.$;
    //实时验证用户名是否存在
    $(function () {
        userNameChang();
    });
    $("#codeBtn").on("click",function (data) {
        var phone = $("#phone").val()
        if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){   //^1 以数字1开头 (3|4|5|6|7|8|9)第二位数 \d(9) 任意9位数 $以数字结尾
            layer.msg("手机号码有误，请重填");
        }else{
            $.ajax({
                type:"post",
                url:"/Ticket/home/mobileCode",
                dataType:"json",
                data:{
                    "phone":phone
                },
                success:function (res) {
                    if(res.code == 0){
                        layer.msg(res.msg)
                    }else{
                        layer.msg(res.msg)
                    }
                }
            })
        }
    });
    $('#agreement').click(function () {
        layer.open({
            type:2,
            title:'用户协议',
            area: ['450px','250px'],
            content:'http://localhost:8081/Ticket/home/msg'
        });
    });
    //表单取值
    layui.$('#LAY-component-form-getval').on('click', function(){
        var data = form.val('example');
        alert(JSON.stringify(data));
    });
    //普通图片上传
    var uploadInst = upload.render({
        elem: '#upImg'
        ,url: '/Ticket/home/upImg' //改成您自己的上传接口
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#headImgUp').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //上传成功
            if(res.code == 0){
                layer.msg("上传成功！")
                document.getElementById("headImg").style.width=160;
                document.getElementById("headImg").style.height=90;
                // $("#headImg").style.width=160;
                // $("#headImg").style.height=90;
                $("#headImg").val(res.msg)
            }
            //如果上传失败
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #ff5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //监听提交
    form.on('submit(register)', function(data){
        var mobileCode = data.field.mobileCode
        //复选框检查
        if($("#agree").is(":checked") === true){
            $.ajax({
                type:'post',
                url:'/Ticket/home/codeVarify',
                dataType:'json',
                data:{
                    "mobileCode":mobileCode
                },
                success:function (res) {
                    if(res.code == 0){
                        $.ajax({
                            type:'post',
                            url:'/Ticket/home/registerOn',
                            dataType:'json',
                            data:data.field,
                            success:function (res) {
                                if(res.code == 0){
                                    layer.open({
                                        title: '提示'
                                        ,content: '注册成功！即将跳转至登录页面！',
                                        time:3000
                                    });
                                    setTimeout(function () {
                                        window.location.href = "/Ticket/home/returnLogin"
                                    },3000)
                                }else{
                                    layer.msg(res.msg)
                                }
                            }
                        })
                    }else{
                        layer.msg(res.msg)
                    }
                }
            })
        }else {
            layer.msg("请先阅读《用户协议》！")
        }
        return false;
    });

});
//实时验证用户名是否重复
function userNameChang() {
    var username=document.getElementById('username');
    if("\v"=="v")//验证是否是ie
    {
        var value=username.onpropertychange;
        console.log("输入框的值"+value);
    }
    else {
        username.addEventListener("input",function (event) {
            var name=username.value;
            $.ajax({
                type:'post',
                url:'/Ticket/home/userNameChange?time='+new Date().getTime(),
                dataType:'json',
                data:{'value':name},
                success:function (res) {
                    if(res.code!=0)
                    {
                        $('#usernameUp').text("该用户名已被使用");
                    }
                    else $('#usernameUp').text("");
                }
            })
        });
    }
}
